package com.zsrt.devbase.common.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
/**
 * 登录日志拦截器，后期改用切面记录日志
 * @author leixin
 * @Date 2017年4月26日
 * @version 1.0
 */
public class LogUserOperationInterceptor implements HandlerInterceptor {

	@Value("${authInterceptor.info.no.valid.url}")
	private String findPrivilege;

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {

	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {

	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		String uri = null;
		this.addSysLog(request, uri);
		return true;
	}

	/**
	 * 插入系统日志
	 * 
	 * @param request
	 * @param uri
	 */
	private void addSysLog(final HttpServletRequest request, String uri) {
		/*	HttpSession session = request.getSession();
		LogUserInfoDTO logUserOperation = new LogUserInfoDTO();
		// 插入登录日志
		if (session.getAttribute(ConstantDefine.PERSONCODE_SESSION)!=null) {
			logUserOperation.setCreator((String) session.getAttribute(ConstantDefine.PERSONCODE_SESSION));
			logUserOperation.setModifier((String) session.getAttribute(ConstantDefine.PERSONCODE_SESSION));
			logUserOperation.setPersonCode((String) session.getAttribute(ConstantDefine.PERSONCODE_SESSION));
		}else {
//			logUserOperation.setPersonCode("anonymous");
			logUserOperation.setCreator("anonymous");
			logUserOperation.setModifier("anonymous");
		}
		
		String loginDate = DateUtil.getSystemTimeStr();
		logUserOperation.setLoginDate(loginDate);
		logUserOperation.setUserOptions(uri);
		logUserOperation.setLogIp(NetworkUtil.getIpAddress(request));
		logUserOperation.setState("1");
		logUserOperation.setCreateTime(DateUtil.getSystemTimeStr());
		try {
			new Thread(){
				public void run() {
					//获取Ip的mac地址
					String macAddress = GetMacAddress.getMacAddress(NetworkUtil.getIpAddress(request));
					//将mac地址存入session中
					ShiroUtil.setSession("macAddress", macAddress);
				};
			}.start();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		logUserOperation.setModifyTime(DateUtil.getSystemTimeStr());
		String macAddress = (String) ShiroUtil.getSessionByKey("macAddress");
		if (null == macAddress || "".equals(macAddress)) {
			logUserOperation.setLogMac("00-00-00-00-00-00");// 没有获取到mac地址
		} else {
			logUserOperation.setLogMac(macAddress);
		}

		//logUserOperationService.insertLogUserOperation(logUserOperation);
*/
	}

}
